home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / CMP.C < prev    next >
Text File  |  1992-12-02  |  6KB  |  237 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  */
  55.  
  56. #include    "SystemPub.h"
  57. #include    "Proc.h"
  58. #include    "ShellPub.h"
  59. #include    "Path.h"
  60.  
  61. #define        BUFSIZE        256
  62.  
  63. /*******************************************************************
  64.  *
  65.  *    Function CMP
  66.  *
  67.  *    cmp [options] file1 file2
  68.  *    
  69.  *    cmp compares two files.  If file1 is '-' then the standard input is used.
  70.  *    If the files are the same cmp does nothing. If they differ the byte offset
  71.  *    and differing bytes are printed.
  72.  *    
  73.  *    options
  74.  *        -l        print the byte offset and the bytes for each difference.
  75.  *        -s        print nothing for differing files, output return code only.
  76.  *
  77.  *******************************************************************/
  78.  
  79. #define        cmpLong        (**MyShell).Proc[ProcID].bflags.f0
  80. #define        cmpSilent    (**MyShell).Proc[ProcID].bflags.f1
  81. #define        cmpAbort    (**MyShell).Proc[ProcID].bflags.f2
  82.  
  83.  
  84. void        CMPFile( WHandle ShellWh, int ProcID, char *file1, char *file2 )
  85. {
  86. int16        aRefNum,     bRefNum,
  87.             aErr,         bErr,
  88.             aLineNum,    bLineNum,
  89.             filesCmp = TRUE,
  90.             cantCmp = TRUE,
  91.             diffFound = FALSE;
  92. char        aBuf[ BUFSIZE ],
  93.             bBuf[ BUFSIZE ];
  94. int32        offset = 0;
  95. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  96.  
  97.     aRefNum = OpenFileDirect( file1, 'TEXT', fsRdPerm );
  98.     
  99.     if( aRefNum )
  100.         {
  101.         bRefNum = OpenFileDirect( file2, 'TEXT', fsRdPerm );
  102.         
  103.         if( bRefNum )
  104.             {
  105.             aLineNum = 1;        bLineNum = 1;
  106.             cantCmp = FALSE;
  107.             
  108.             while( filesCmp && !cmpAbort )
  109.                 {
  110.                 if( UserAbort() )
  111.                     cmpAbort = TRUE;
  112.                     
  113.                 aErr = ReadLine( aRefNum, aBuf, BUFSIZE );
  114.                 bErr = ReadLine( bRefNum, bBuf, BUFSIZE );
  115.                 
  116.                 if( aErr || bErr )
  117.                     break;
  118.                 
  119.                 if( strcmp( aBuf, bBuf ) )    /* did not compare */
  120.                     {
  121.                     if( !cmpSilent )
  122.                         procPrintf(  ShellWh, ProcID, "Mismatch line %d\n%s%s",
  123.                             aLineNum, aBuf, bBuf );
  124.                     
  125.                     filesCmp = cmpLong;
  126.                     diffFound = TRUE;
  127.                     }
  128.                     
  129.                 offset += (int32) strlen( aBuf );                
  130.                 aLineNum++;
  131.                 bLineNum++;
  132.                 }
  133.             
  134.             FSClose( aRefNum );
  135.             FSClose( bRefNum );
  136.             }
  137.         else
  138.             FSClose( aRefNum );
  139.         }
  140.         
  141.     if( cantCmp )
  142.         procPrintf( ShellWh, ProcID, "cmp : can't compare %s and %s\n",
  143.             file1, file2 );
  144.     else
  145.         if( cmpSilent )
  146.             procPrintf( ShellWh, ProcID, "%d\n", diffFound );
  147.             
  148.     ResetShellPWD( ShellWh );
  149. }
  150.  
  151. /*******************************************************************/
  152.  
  153. Boolean            DoCMP( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  154. {
  155. int16            i, argc;
  156. char            *cp, arg1[ 256 ], arg2[ 256 ];
  157. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  158.  
  159.     switch( ProcToken )
  160.         {
  161.         case    PROC_INIT    :
  162.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  163.             break;
  164.             
  165.         case    PROC_TERM    :
  166.         case    PROC_BREAK    :
  167.             cmpAbort = TRUE;
  168.             /* Tell the shell that we're done */
  169.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  170.             /* Turn ourself off */
  171.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  172.             break;
  173.             
  174.         case    PROC_STDIN    :
  175.             if( (**MyShell).Proc[ ProcID ].flags )
  176.                 {
  177.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  178.                 cmpAbort = FALSE;
  179.                 cmpLong = FALSE;
  180.                 cmpSilent = FALSE;
  181.                 
  182.                 /* get arguments */
  183.                 argc = (**MyShell).Proc[ ProcID ].argc;
  184.                 for( i = 1; i < argc; i++ )
  185.                     {
  186.                     GetArgv( ShellWh, ProcID, i, arg1 );
  187.                     cp = arg1;
  188.         
  189.                     if( *cp++ == '-' )
  190.                         while( *cp )
  191.                             switch( *cp++ )
  192.                                 {
  193.                                 case    'l'    :    /* long list of compare */
  194.                                     cmpLong = TRUE;
  195.                                     break;
  196.                                     
  197.                                 case    's'    :    /* silent mode */
  198.                                     cmpSilent = TRUE;
  199.                                     break;
  200.                                 }
  201.                     }
  202.  
  203.                 *arg1 = '\0';
  204.                 *arg2 = '\0';
  205.                 
  206.                 for( i = 1; i < argc; i++ )
  207.                     {
  208.                     if( *arg1 == '\0' )
  209.                         {
  210.                         GetArgv( ShellWh, ProcID, i, arg1 );
  211.                         if( *arg1 == '-' )
  212.                             *arg1 = '\0';
  213.                         }
  214.                     else if( *arg2 == '\0' )
  215.                         {
  216.                         GetArgv( ShellWh, ProcID, i, arg2 );
  217.                         if( *arg2== '-' )
  218.                             *arg2 = '\0';
  219.                         else
  220.                             break;
  221.                         }
  222.                     }
  223.                     
  224.                 if( *arg1 && *arg2 )
  225.                     CMPFile( ShellWh, ProcID, arg1, arg2 );
  226.  
  227.                 /* Tell the shell that we're done */
  228.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  229.                 
  230.                 /* Turn ourself off */
  231.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  232.                 return( FALSE );
  233.                 }
  234.         }
  235. }
  236.  
  237.